/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hadss_adaptive_video/hadss_adaptive_video.dart';

void main() {
  group('Adaptive video immersion test', () {
    late AdaptiveImmersion adaptiveImmersion;
    const bottomTabHeight = 56.0;
    setUp(() {
      adaptiveImmersion = AdaptiveImmersion();
    });
    test('should be a singleton', () {
      expect(adaptiveImmersion, same(AdaptiveImmersion()));
    });
    group('ImmersionInfo for different video and window sizes', () {
      const EdgeInsets windowPadding = EdgeInsets.only(top: 24);
      testWidgets('Rule 1: test 9:16 video with window ratio 0 <= x < 9:20',
          (widgetTester) async {
        const Size windowSize = Size(414, 966);
        const Size videoSize = Size(1080, 1920);
        ImmersionInfo? immersionInfo;
        await widgetTester.pumpWidget(MaterialApp(
            home: MediaQuery(
          data: const MediaQueryData(size: windowSize, padding: windowPadding),
          child: Builder(
            builder: (context) {
              immersionInfo = adaptiveImmersion.getImmersionInfo(
                  context, videoSize, bottomTabHeight);
              return Container();
            },
          ),
        )));
        expect(immersionInfo?.immersionSetting.isStatusBarImmersive, false);
        expect(immersionInfo?.immersionSetting.isBottomTabImmersive, false);
        double width = immersionInfo?.videoSize.width ?? 0;
        double height = immersionInfo?.videoSize.height ?? 1;
        expect(width / height, 9 / 16);
      });
      testWidgets('Rule 2: test 9:16 video with window ratio 9:20 <= x < 9:18',
          (widgetTester) async {
        const Size windowSize = Size(375, 812);
        const Size videoSize = Size(1080, 1920);
        ImmersionInfo? immersionInfo;
        await widgetTester.pumpWidget(MaterialApp(
            home: MediaQuery(
          data: const MediaQueryData(size: windowSize, padding: windowPadding),
          child: Builder(
            builder: (context) {
              immersionInfo = adaptiveImmersion.getImmersionInfo(
                  context, videoSize, bottomTabHeight);
              return Container();
            },
          ),
        )));
        expect(immersionInfo?.immersionSetting.isStatusBarImmersive, true);
        expect(immersionInfo?.immersionSetting.isBottomTabImmersive, false);
      });
      testWidgets(
          'Rule 3: test 9:16 video with window ratio 9:18 <= x < 9:14.4',
          (widgetTester) async {
        const Size windowSize = Size(520, 900);
        const Size videoSize = Size(1080, 1920);
        ImmersionInfo? immersionInfo;
        await widgetTester.pumpWidget(MaterialApp(
            home: MediaQuery(
          data: const MediaQueryData(size: windowSize, padding: windowPadding),
          child: Builder(
            builder: (context) {
              immersionInfo = adaptiveImmersion.getImmersionInfo(
                  context, videoSize, bottomTabHeight);
              return Container();
            },
          ),
        )));
        expect(immersionInfo?.immersionSetting.isStatusBarImmersive, true);
        expect(immersionInfo?.immersionSetting.isBottomTabImmersive, true);
      });
      testWidgets('Rule 4: test 9:16 video with window ratio 9:14.4 <= x',
          (widgetTester) async {
        const Size windowSize = Size(540, 720);
        const Size videoSize = Size(1080, 1920);
        ImmersionInfo? immersionInfo;
        await widgetTester.pumpWidget(MaterialApp(
            home: MediaQuery(
          data: const MediaQueryData(size: windowSize, padding: windowPadding),
          child: Builder(
            builder: (context) {
              immersionInfo = adaptiveImmersion.getImmersionInfo(
                  context, videoSize, bottomTabHeight);
              return Container();
            },
          ),
        )));
        expect(immersionInfo?.immersionSetting.isStatusBarImmersive, true);
        expect(immersionInfo?.immersionSetting.isBottomTabImmersive, true);
      });
      testWidgets('Rule 5: test other video with window ratio',
          (widgetTester) async {
        const Size windowSize = Size(540, 720);
        const Size videoSize = Size(1080, 1080);
        ImmersionInfo? immersionInfo;
        await widgetTester.pumpWidget(MaterialApp(
            home: MediaQuery(
          data: const MediaQueryData(size: windowSize, padding: windowPadding),
          child: Builder(
            builder: (context) {
              immersionInfo = adaptiveImmersion.getImmersionInfo(
                  context, videoSize, bottomTabHeight);
              return Container();
            },
          ),
        )));
        expect(immersionInfo?.immersionSetting.isStatusBarImmersive, false);
        expect(immersionInfo?.immersionSetting.isBottomTabImmersive, false);
      });
    });
  });

  group('Adaptive video rotation test', () {
    late AdaptiveRotation adaptiveRotation;
    setUp(() {
      adaptiveRotation = AdaptiveRotation();
    });
    test('should be a singleton', () {
      expect(adaptiveRotation, same(AdaptiveRotation()));
    });
  });
}
